package week_five;

public class RelativeSortArray_1122 {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int[] help = new int[1001];
        for (int i = 0; i < arr1.length; i++) {
            help[arr1[i]]++;
        }
        int i = 0;
        for (int j = 0; j < arr2.length; j++) {
            int count = help[arr2[j]];
            for (int x = 0; x < count; x++) {
                arr1[i++] = arr2[j];
            }
            help[arr2[j]] = 0;
        }

        for (int y = 1; y < 1001; y++) {
            int count = help[y];
            if (count > 0) {
                for (int x = 0; x < count; x++) {
                    arr1[i++] = y;
                }
            }
        }
        return arr1;

    }
}
